import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  //获取各种环境下的对应的变量
  let env = loadEnv(mode, process.cwd())
  return {
    plugins: [
      vue()
    ],
    resolve: {
      alias: {
        '@': path.resolve('./src'),
      },
    },
    //代理跨域
    server: {
      proxy: {
        [env.VITE_APP_BASE_API]: {
          target: env.VITE_SERVE,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ''), // 替换/api为空字符串
          bypass: (req, res, proxyOptions) => {
            const proxyUrl =
                new URL(
                    proxyOptions.rewrite!(req.url) || '',
                    proxyOptions.target as string,
                )?.href || ''
            console.log(proxyUrl)
          },
        },
      },
    },
  }
})
